package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.BusinessCardApplyListDTO;
import com.oa.admin.entity.BusinessCardApply;
import com.oa.admin.service.IBusinessCardApplyService;
import com.oa.admin.vo.BusinessCardApplyDetailsVO;
import com.oa.admin.vo.BusinessCardApplyListVO;
import com.oa.admin.dto.BusinessCardApplyAddDTO;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/businessCardApply")
@Api(description = "名片申请表模块")
public class BusinessCardApplyController extends BaseController {

    private final IBusinessCardApplyService businessCardApplyService;

    @Autowired
    public BusinessCardApplyController(IBusinessCardApplyService businessCardApplyService) {
        this.businessCardApplyService = businessCardApplyService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询列表")
    @ApiOperation(value = "查询")
    public Resp<BusinessCardApplyListVO> findBusinessCardApplyListRecord(BusinessCardApplyListDTO businessCardApplyListDTO) {
        businessCardApplyListDTO.setUserId(getLoginUserId());
        return new Resp().success(businessCardApplyService.findBusinessCardApplyList(businessCardApplyListDTO));
    }

    @GetMapping("/export")
    @CommonController
    @Description("导出")
    @ApiOperation(value = "导出")
    public void export(BusinessCardApplyListDTO businessCardApplyListDTO) {
        businessCardApplyListDTO.setUserId(getLoginUserId());
        businessCardApplyService.export(businessCardApplyListDTO, response);
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<BusinessCardApplyDetailsVO> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(businessCardApplyService.findBusinessCardApplyById(id));
    }


    /**
     * 新增
     *
     * @param businessCardApplyAddDTO BusinessCardApplyAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "名片申请表对象") @RequestBody(required = false) BusinessCardApplyAddDTO businessCardApplyAddDTO) throws DataIsEmptyException, BusinessException {
        businessCardApplyAddDTO.setProposer(getLoginUserId());
        return businessCardApplyService.addBusinessCardApply(businessCardApplyAddDTO);
    }

    /**
     * 根据Id修改
     *
     * @param businessCardApply BusinessCardApply
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "名片申请表对象") @RequestBody(required = false) BusinessCardApply businessCardApply) {
        if (businessCardApplyService.updateById(businessCardApply)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        BusinessCardApply businessCardApply = businessCardApplyService.selectById(id);
        if (businessCardApply != null && businessCardApply.getIsDeleted() == 2) {
            businessCardApply.setIsDeleted(1);
            if (businessCardApplyService.updateById(businessCardApply)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
